package tools

import (
	crand "crypto/rand"
	"fmt"
	"math/big"
	mrand "math/rand"
	"sort"
)

func CRand(min, max int64) (int64, error) {
	n, err := crand.Int(crand.Reader, big.NewInt(max-min))
	if err != nil {
		return 0, err
	}
	return n.Int64() + min, nil
}

// 伪随机性能是真随机10倍左右
func MRand(min, max int) int {
	n := mrand.Intn(max - min)
	return n + min
}

// 生成指定个数的随机字符串,有重复
func RandomString(n int, allowedChars ...[]rune) string {
	var defaultLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	var letters []rune

	if len(allowedChars) == 0 {
		letters = defaultLetters
	} else {
		for _, allowedChar := range allowedChars {
			letters = append(letters, allowedChar...)
		}
	}

	b := make([]rune, n)
	for i := range b {
		b[i] = letters[mrand.Intn(len(letters))]
	}

	return string(b)
}

// 获取最小值,最大值
func GetMinMax[T int | string | float64](s []T) (min T, max T) {
	switch any(s).(type) {
	case []int:
		sort.Ints(any(s).([]int))
	case []string:
		sort.Strings(any(s).([]string))
	case []float64:
		sort.Float64s(any(s).([]float64))
	}
	return s[0], s[len(s)-1]
}

func RandA() {

	// var seed int64
	// binary.Read(crand.Reader, binary.BigEndian, &seed)
	// fmt.Println(seed)

	//m := mrand.New(mrand.NewSource(100))

	//系统默认获取种子，每次运行结果都一样
	for i := 0; i < 10; i++ {
		n := mrand.Intn(100)
		fmt.Println(n)
	}
}
